

package com.css.gdzj.gdzjbxgd.modules.reportcontral.service.impl;

import com.css.gdzj.gdzjbxgd.common.util.R;
import com.css.gdzj.gdzjbxgd.common.util.ServiceUtil;
import com.css.gdzj.gdzjbxgd.common.util.SnowflakeId;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.entity.BxgdReportContralEntity;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.entity.QBxgdReportContralEntity;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.repository.BxgdReportContralRepository;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.service.BxgdReportContralService;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import com.css.gdzj.gdzjbxgd.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 填报时间控制
 * @author zz
 */
@Service
public class BxgdReportContralServiceImpl implements BxgdReportContralService {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    HttpServletRequest request;
    @Autowired
    ServiceUtil serviceUtil;
    @Autowired
    BxgdReportContralRepository bxgdReportContralRepository;

    @Override
    public Map<String,Object> getList(String reportYear,Integer pageNo, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        Map<String,Object> map = new HashMap<>();
        QBxgdReportContralEntity qBxgdReportContralEntity = QBxgdReportContralEntity.bxgdReportContralEntity;
        List<BxgdReportContralEntity> list = new ArrayList<BxgdReportContralEntity>();
        Predicate predicate = qBxgdReportContralEntity.isNotNull().or(qBxgdReportContralEntity.isNull());
        predicate = ExpressionUtils.and(predicate, qBxgdReportContralEntity.flag.goe("0")) ;
        //0:已删除 1：无效 2：有效
        if (!StringUtils.isEmpty(reportYear)) {
            list = jpaQueryFactory.selectFrom(qBxgdReportContralEntity)
                    .where(qBxgdReportContralEntity.reportYear.eq(reportYear),qBxgdReportContralEntity.flag.goe("0")).fetch();
            predicate = ExpressionUtils.and(predicate, qBxgdReportContralEntity.reportYear.eq(reportYear)) ;
        }else {
            list = jpaQueryFactory.selectFrom(qBxgdReportContralEntity)
                    .where(qBxgdReportContralEntity.flag.goe("0")).fetch();
        }
        QueryResults<BxgdReportContralEntity> results = jpaQueryFactory
                .selectFrom(qBxgdReportContralEntity)
                .where(
                        predicate
                )
                .orderBy(qBxgdReportContralEntity.reportYear.desc())
                .orderBy(qBxgdReportContralEntity.updateTime.desc())
                .offset(pageRequest.getOffset())   //起始页
                .limit(pageRequest.getPageSize())  //每页大小
                .fetchResults();
        map.put("list",results.getResults());
        map.put("total",list.size());
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BxgdReportContralEntity saveBxgdReportContral(BxgdReportContralEntity bxgdReportContralEntity) {
        String uuid = SnowflakeId.getId();
        bxgdReportContralEntity.setUuid(uuid);
        R r = serviceUtil.getUserInfoByRequest(request);
        List<BxgdReportContralEntity> list = new ArrayList<BxgdReportContralEntity>();
        QBxgdReportContralEntity qBxgdReportContralEntity = QBxgdReportContralEntity.bxgdReportContralEntity;
        list = jpaQueryFactory.selectFrom(qBxgdReportContralEntity)
                .where(qBxgdReportContralEntity.flag.eq("2")).fetch();
        if (list != null && list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                BxgdReportContralEntity bxgdReport= list.get(i);
                bxgdReport.setFlag("1");
                this.bxgdReportContralRepository.save(bxgdReport);
            }
        }
        if(r.getCode().equals("200")){
            UserInfoEntity userInfo =  (UserInfoEntity)r.get("data");
            bxgdReportContralEntity.setCreateUser(userInfo.getUserId());
            bxgdReportContralEntity.setCreateTime(new Date());
            bxgdReportContralEntity.setUpdateTime(new Date());
        }
        bxgdReportContralEntity.setFlag("2");
        this.bxgdReportContralRepository.save(bxgdReportContralEntity);
        return bxgdReportContralEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BxgdReportContralEntity updateBxgdReportContral(BxgdReportContralEntity bxgdReportContralEntity) {
        bxgdReportContralEntity.setUpdateTime(new Date());
        if(!bxgdReportContralEntity.getReportTimeEnd().contains("23:59:59")){
            bxgdReportContralEntity.setReportTimeEnd(bxgdReportContralEntity.getReportTimeEnd().concat(" 23:59:59")) ;
        }
        this.bxgdReportContralRepository.save(bxgdReportContralEntity);
        return bxgdReportContralEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBxgdReportContral(List<String> idList) {
        List<BxgdReportContralEntity> bxgdReportContralEntityList = this.bxgdReportContralRepository.findAllById(idList);
        if(!StringUtils.isEmpty(bxgdReportContralEntityList) && bxgdReportContralEntityList.size() > 0) {
            for(BxgdReportContralEntity bxgdReportContralEntity : bxgdReportContralEntityList) {
                bxgdReportContralEntity.setFlag("0");
                this.bxgdReportContralRepository.save(bxgdReportContralEntity);
            }
        }
    }

    @Override
    public BxgdReportContralEntity selectLimitTime() {
        return bxgdReportContralRepository.selectLimitTime();
    }
}
